//
//  CHAddressCell.m
//  CarFile
//
//  Created by 陈浩 on 2017/12/18.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHAddressCell.h"

@interface CHAddressCell()
{
    UILabel *markLabel;
}

@end
@implementation CHAddressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+(instancetype)cellForTableView:(UITableView *)tableView{
    static NSString *ID = @"CHAddressCell";
   
      CHAddressCell  * cell = [[CHAddressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self initView];
    }
    return self;
}

-(void)initView{
    
    markLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    markLabel.textAlignment = NSTextAlignmentCenter;
    markLabel.font =Font(14);
    markLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    [self addSubview:markLabel];
    
}


-(void)setSectionTitle:(NSString *)sectionTitle{
    _sectionTitle = sectionTitle;
    markLabel.text = sectionTitle;
}

-(void)setTitles:(NSArray *)titles{
    _titles = titles;
    
    CGFloat w = (UIScreenW - 30 -40)/4;
    for (int i = 0; i < titles.count; i++) {
        ProvinceModel *model = titles[i];
        NSInteger row = i /4;
        NSInteger col = i %4;
        CGFloat x = 30+ (w + 10)*col;
        CGFloat y = (30 + 15)*row;
        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i;
        btn.titleLabel.font = Font(14);
        [btn setTitle:model.name forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        btn.layer.borderWidth = 1;
        btn.layer.borderColor = [UIColor colorWithHexString:@"d4d4d4" alpha:1.0f].CGColor;
        btn.frame = CGRectMake(x, y, w, 30);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];

    }
}

+(CGFloat)getCellHeightOfCityArray:(NSArray *)cityArray{

    NSInteger row = (cityArray.count-1) /4;
    CGFloat h = 30+(30+15)*row;
    
    return h;
    
}

-(void)btnClick:(UIButton *)btn{
    
    ProvinceModel *model = self.titles[btn.tag];
    if (self.block) {
        self.block(model);
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
